package com.myapp.ruyixing.ui;

import static android.view.View.GONE;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.myapp.ruyixing.Config;
import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.CityList;
import com.myapp.ruyixing.model.CityList.City;
import com.myapp.ruyixing.ui.widget.TitleBar;

public class DishListActivity extends Activity implements OnItemClickListener,
		OnClickListener {

	private CityListAdapter mAdapter;
	private ArrayList<City> mCityList;
	private ListView mListView;

	private RYHttpResponseHandler mHttpResponseHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			try {
				CityList citylist = (CityList) o;
				onLoadingDataFinished(true, citylist);
			} catch (Exception e) {
				onLoadingDataFinished(false, null);
			}
		}

		@Override
		public void onFailure(Throwable throwable) {
			onLoadingDataFinished(false, null);
		}
	};

	private void showToast(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}

	private void showLoading(boolean show, String msg) {
		if (isFinishing()) {
			return;
		}
		if (show) {
			findViewById(R.id.layout_loading).setVisibility(View.VISIBLE);
			((TextView) findViewById(R.id.text_loading)).setText(msg);
		} else {
			findViewById(R.id.layout_loading).setVisibility(GONE);
		}
	}

	private void onLoadingDataStarted() {
		showLoading(true, "加载中");
		RYHttpClient.getCityList(this, mHttpResponseHandler);
	}

	private void onLoadingDataFinished(boolean succeed, CityList citylist) {
		showLoading(false, "");
		if (succeed && citylist != null) {
			if (citylist.list != null && citylist.list.size() > 0) {
				mCityList.clear();
				mCityList.addAll(citylist.list);
				mAdapter.notifyDataSetChanged();
				for (City city : citylist.list) {
				}
			}
		} else {
			showToast("加载城市列表失败！");
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_city_list);
		((TitleBar) findViewById(R.id.title_bar))
				.setOnIconClickedListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						onBackPressed();
					}
				});
		mListView = (ListView) findViewById(R.id.list);
		mCityList = new ArrayList<City>();
		mAdapter = new CityListAdapter();
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(this);
		// onLoadingDataStarted();
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long id) {
		if (id >= 0 && mCityList != null && id < mCityList.size()) {
			setCity(mCityList.get((int) id).cityname,
					mCityList.get((int) id).cityid);
			finish();
		}
	}

	private class CityListAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return mCityList == null ? 0 : mCityList.size();
		}

		@Override
		public Object getItem(int position) {
			return mCityList == null ? null : mCityList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			City city = (City) getItem(position);
			if (city == null) {
				return convertView;
			}
			if (convertView == null) {
				convertView = LayoutInflater.from(DishListActivity.this)
						.inflate(R.layout.text_city, parent, false);
			}
			if (!TextUtils.isEmpty(city.cityname)) {
				((TextView) convertView).setText(city.cityname);
			}
			return convertView;
		}
	}

	public static boolean isCitySet(Context context) {
		SharedPreferences preferences = context.getSharedPreferences("city",
				Context.MODE_MULTI_PROCESS);
		return preferences.getBoolean("set", false);
	}

	public static String getCityName(Context context) {
		SharedPreferences preferences = context.getSharedPreferences("city",
				Context.MODE_MULTI_PROCESS);
		return preferences.getString("cityname", "");
	}

	public static String getCityId(Context context) {
		SharedPreferences preferences = context.getSharedPreferences("city",
				Context.MODE_MULTI_PROCESS);
		return preferences.getString("cityid", "");
	}

	private void setCity(String cityname, String cityid) {
		Config.sCityName = cityname;
		Config.sCityId = cityid;
		SharedPreferences preferences = getSharedPreferences("city",
				Context.MODE_MULTI_PROCESS);
		Editor editor = preferences.edit();
		editor.putBoolean("set", true);
		editor.putString("cityname", cityname);
		editor.putString("cityid", cityid);
		editor.commit();
	}

	@Override
	public void onClick(View v) {
	}

}
